import {
	window,
	commands,
	workspace,
	ExtensionContext,
} from "vscode";

import Gist from "./gist";

async function check() {
	const tokenType = workspace.getConfiguration("gist").get("web") === "github" ? "githubToken" : "giteeToken";
	if (!workspace.getConfiguration("gist").get(tokenType)) {
		window.showInformationMessage("您尚未配置access_token，请在上方输入框内输入access_token回车继续。");
		const token = await window.showInputBox({ placeHolder: `请填写access_token，当前类型为(${ tokenType })` });
		if (token) {
			await workspace.getConfiguration("gist").update(tokenType, token);
			return true;
		}
	}
	return true;
}

export function activate(context: ExtensionContext) {

	const gist = new Gist();

	context.subscriptions.push(commands.registerCommand("gist.createBySelect", async () => {
		if (await check()) {
			gist.createBySelect();
		}
	}));

	context.subscriptions.push(commands.registerCommand("gist.addBySelect", async () => {
		if (await check()) {
			gist.addBySelect();
		}
	}));

	context.subscriptions.push(commands.registerCommand("gist.delete", async () => {
		if (await check()) {
			gist.delete();
		}
	}));
	
	context.subscriptions.push(commands.registerCommand("gist.deleteFile", async () => {
		if (await check()) {
			gist.deleteFile();
		}
	}));

	context.subscriptions.push(commands.registerCommand("gist.getList", async () => {
		if (await check()) {
			gist.getList();
		}
	}));

	context.subscriptions.push(commands.registerCommand("gist.getPublicList", () => {
		gist.getPublicList();
	}));

	context.subscriptions.push(commands.registerCommand("gist.edit", async () => {
		if (await check()) {
			gist.edit();
		}
	}));

	context.subscriptions.push(commands.registerCommand("gist.createByFile", async ({ fsPath: path }) => {
		if (await check()) {
			gist.createByFile(path);
		}
	}));

	context.subscriptions.push(commands.registerCommand("gist.addByFile", async ({ fsPath: path }) => {
		if (await check()) {
			gist.addByFile(path);
		}
	}));
}